import JsNativeBridge from "./JsNativeBridge";

//屏幕方向，小游戏中只有两种。竖屏和左横屏
export  enum OrientationType{
    Portrait,
    Landscape,
    RightLandscape, // 暂时还不支持。
};

export default class Orientation{

    private static _isOrientationH(val:OrientationType){
        var frameSize = cc.view.getFrameSize();
        cc.log("frameSize.height  ",frameSize.height," frameSize.width  ",frameSize.width)
        if(val){
            return frameSize.height < frameSize.width;
        }else{
            return frameSize.height > frameSize.width;
        }
        
    }

    // true 是切换到横屏false 是切换到竖屏
    static changeOrientation(val:OrientationType,cb) {

        if(this._isOrientationH(val)){
            cc.log(" 已经是竖屏游戏")
            return;
        }
        cc.log(" ============  isMobile cc.sys.isMobile ",cc.sys.isMobile)
        if(cc.sys.isBrowser){

            if(val == 1){
                cc.view.setOrientation(cc.macro.ORIENTATION_LANDSCAPE);
            }else{
                cc.view.setOrientation(cc.macro.ORIENTATION_PORTRAIT);
            }     
            // let frameSize = cc.view.getFrameSize();   
            // cc.log(" =====================================)))))))))))))))))))))) 22222",frameSize.height,frameSize.width);         
            // cc.view.setFrameSize(frameSize.height,frameSize.width);                        
            if(cb){
                cb();
            }  
        }else{

            let className = "org/cocos2dx/javascript/AppActivity";
            let methodName = "changeOrientationH";
            cc.log(" cc.sys.os  ",cc.sys.os );
            if(cc.sys.os == cc.sys.OS_IOS){
                className = "AppController";
                methodName = "changeOrientationH:"
                // jsb.reflection.callStaticMethod(
                //    "AppController",                   
                //     "changeOrientationH:",                    
                //     val );            
            }
        //     else if(cc.sys.os == cc.sys.OS_ANDROID){
        //         jsb.reflection.callStaticMethod(
        //             "org/cocos2dx/javascript/AppActivity",                    
        //              "changeOrientationH",                    
        //             "(Z)V",                   
        //              val );          
        //    }

           JsNativeBridge.callStaticMethod(className,methodName,val,"(I)V");
           
           if(val == 1){
                cc.view.setOrientation(cc.macro.ORIENTATION_LANDSCAPE);
            }else{
                cc.view.setOrientation(cc.macro.ORIENTATION_PORTRAIT);
            }               
           if(cb){
            // var timeID = setTimeout(()=>{
            //     clearTimeout(timeID);
                // let frameSize = cc.view.getFrameSize();   
                // cc.log(" =====================================)))))))))))))))))))))) 22222",frameSize.height,frameSize.width);         
                // cc.view.setFrameSize(frameSize.height,frameSize.width);                   
                cb();
            // },500);
            }           
        }


    }
}